package com.example.yzx.allriddle;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.*;
import com.bumptech.glide.Glide;
import com.example.yzx.allriddle.daomain.Sort;
import com.example.yzx.allriddle.utils.Db;
import com.example.yzx.allriddle.utils.SortAdapter;
import com.example.yzx.allriddle.utils.SoundPlayer;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import java.util.ArrayList;
import java.util.List;

import static com.example.yzx.allriddle.utils.SoundPlayer.startMusic;


public class MainActivity extends AppCompatActivity implements OnBannerListener {
    private String[] list={"","","","",""};
    private int[] listimg={R.mipmap.s1,R.mipmap.s2,R.mipmap.s3,R.mipmap.s4,R.mipmap.s5};
    private Db db = new Db(this, 1);
    private Banner mBanner;
    private LinearLayout lout;
    private   SoundPlayer soundPlayer=new SoundPlayer();
    private MyImageLoader mMyImageLoader;
    private ImageView img1;
    private ArrayList<Integer> imagePath;
    private ArrayList<String> imageTitle;
    private List<Sort> lists=new ArrayList<>();
    private int colors=1;
    private int sign=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initData();
        initView();
         lout=findViewById(R.id.lout);
        /*ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.px,list);
        ListView listView=findViewById(R.id.listview);
        listView.setAdapter(adapter);*/

        Sort sort;
        for(int i=0;i<list.length;i++){
            System.out.println(listimg[i]+","+list[i]);
            sort=new Sort(listimg[i],list[i]);
            lists.add(sort);
        }
        SortAdapter sortAdapter=new SortAdapter(MainActivity.this,R.layout.fruit,lists);
        ListView listView=findViewById(R.id.listview);
        listView.setAdapter(sortAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AlertDialog alertDialog2 = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("第 "+(i+1)+" 名" )//标题
                        .setMessage(" ")//内容
                        .create();
                alertDialog2.show();
            }
        });
        Bundle bundle = this.getIntent().getExtras();
        String user=null;
        if(bundle!=null&&bundle.getString("user")!=null){
            user=bundle.getString("user");
        }
        if(user==null||!user.equals("yes")){
            Intent intent=new Intent(MainActivity.this,LoginActivity.class);
            startActivity(intent);
        }
    }
    //通过getMenuInflater()方法得到MenuInflater对象
    public boolean onCreateOptionsMenu(Menu menu){
    //调用inflate()方法创建菜单
        getMenuInflater().inflate(R.menu.menu,menu);
        //如果返回false，创建的菜单无法显示
        return true;
    }
   //菜单栏
    public boolean onOptionsItemSelected(MenuItem item){
        String aboutme="学号：Z9416229   姓名：杨志学 功能：休闲娱乐 版本号：V1.0.1";
        if(sign==1) {
            soundPlayer.init(this);
            sign=0;
        }
        //通过调用item.getItemId()来判断菜单项
        switch (item.getItemId()){
            case R.id.bg:
                bgchange();
                break;
            case R.id.music:
                playmusic();
                break;
            case R.id.stopmusic:
                pusemusic();
                break;
            case R.id.stoplogin:
                nologin();
                break;
            case R.id.from:
                Toast.makeText(this,aboutme,Toast.LENGTH_SHORT).show(); soundPlayer.boom();
                break;
            default:
        }
        return true;
    }
    //退出登录
    public void nologin(){
        SharedPreferences userSettings = getSharedPreferences("setting", 0);
        SharedPreferences.Editor editor = userSettings.edit();
        editor.clear();
        editor.commit();
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
    }
    //播放音乐
    public void playmusic(){

      soundPlayer.startMusic();
    }
   //关闭音乐
    public void pusemusic(){
        soundPlayer.pauseMusic();
    }
    //切换背景色
    public void bgchange(){
        soundPlayer.boom();
        if(colors==1) {
            lout.setBackgroundColor(Color.rgb(0, 255, 255));
            colors=0;
        } else{
            lout.setBackgroundColor(Color.rgb(255, 255, 255));
            colors=1;
        }
    }
    //跳转到阅读
    public void read(View view){
        Intent intent=new Intent(MainActivity.this,ReadActivity.class);
        startActivity(intent);
    }
    //跳转到收藏
    public void star(View view){
        Intent intent=new Intent(MainActivity.this,StarActivity.class);
        startActivity(intent);
    }
    //跳转到AI
    public void aiquery(View view){
        Intent intent=new Intent(MainActivity.this,AiActivity.class);
        startActivity(intent);
    }
    //跳转到游戏
    public void game(View view){
        Intent intent=new Intent(MainActivity.this,GameActivity.class);
        startActivity(intent);
    }
    //初始化
    public void init(){
        SQLiteDatabase sqLiteDbup = db.getReadableDatabase();
        Cursor c = sqLiteDbup.query("user", new String[]{"uid", "name", "word","grade"}, "", null, null, null,  "grade desc","5");
        int i=0;
        while (c.moveToNext()) {
            Integer id = c.getInt(c.getColumnIndex("uid"));
            String name = c.getString(c.getColumnIndex("name"));
            float grade = c.getFloat(c.getColumnIndex("grade"));
            list[i]="第 "+(i+1)+" 名   "+name+"("+grade+" )";i++;

        }
    }
    private void initData() {
        imagePath = new ArrayList<>();
        imageTitle = new ArrayList<>();
        imagePath.add(R.mipmap.bann1);
        imagePath.add(R.mipmap.bann2);
        imagePath.add(R.mipmap.bann3);
        imagePath.add(R.mipmap.bann4);
        //设置标题
       /* imageTitle.add(null);
        imageTitle.add(null);
        imageTitle.add(null);*/
    }
    //轮播
    private void initView() {
        mMyImageLoader = new MyImageLoader();
        mBanner = findViewById(R.id.banner);
        //设置样式，里面有很多种样式可以自己都看看效果
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        mBanner.setImageLoader(mMyImageLoader);
        //设置轮播的动画效果,里面有很多种特效,可以都看看效果。
        mBanner.setBannerAnimation(Transformer.ZoomOutSlide);
        /*//轮播图片的文字
        mBanner.setBannerTitles(imageTitle);*/
        //设置轮播间隔时间
        mBanner.setDelayTime(3000);
        //设置是否为自动轮播，默认是true
        mBanner.isAutoPlay(true);
        //设置指示器的位置，小点点，居中显示
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //设置图片加载地址
        mBanner.setImages(imagePath)
                //轮播图的监听
                .setOnBannerListener(this)
                //开始调用的方法，启动轮播图。
                .start();

    }

    /**
     * 轮播图的监听
     *
     * @param position
     */

    public void OnBannerClick(int position) {
        if(position==0)
             Toast.makeText(this, "生活不止眼前的苟且还有雪山和天堂", Toast.LENGTH_SHORT).show();
        else if(position==1)
            Toast.makeText(this, "朦胧的美，你永远不懂", Toast.LENGTH_SHORT).show();
        else if(position==2)
            Toast.makeText(this, "用于攀登你，更能品味人生", Toast.LENGTH_SHORT).show();
        else if(position==3)
            Toast.makeText(this, "寂静，仅仅是你内心的孤独", Toast.LENGTH_SHORT).show();
    }


    /**
     * 图片加载类
     */
    private class MyImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context.getApplicationContext())
                    .load(path)
                    .into(imageView);
        }
    }
}
